package com.yuchen.ishop.buyer;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.BuyerAddress;
import com.yuchen.jaxrs.ResponseUtils;
import com.yuchen.jaxrs.Responses;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * Created by Administrator on 15-4-23.
 */
@Transactional
@Path("buyerAddressService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
class BuyerAddressServiceImpl implements BuyerAddressService{

    private final static Logger LOG = Logger.getLogger(BuyerAddressService.class);

    @Autowired
    private BaseDao dao;

    @GET
    @Path("get/{id}")
    @Transactional(readOnly = true)
    public BuyerAddress getById(@PathParam("buyerId")Long id) {
        return dao.fetch(BuyerAddress.class, id);
    }

    @POST
    @Path("insert")
    public Responses insert(BuyerAddress buyerAddress) {
        dao.insert(buyerAddress);
        return ResponseUtils.success();
    }

    @POST
    @Path("update")
    public Responses update(BuyerAddress buyerAddress) {
        dao.updateIgnoreNull(buyerAddress);
        return ResponseUtils.success();
    }

    @DELETE
    @Path("delete/{id}")
    public Responses delete(@PathParam("id") Long id) {
        dao.delete(BuyerAddress.class, id);
        return ResponseUtils.success();
    }

    @GET
    @Path("getByBuyerId/{buyerId}")
    @Transactional(readOnly = true)
    public List<BuyerAddress> getByBuyerId(@PathParam("buyerId")Long buyerId) {
        Condition cnd = Cnd.where("buyerId", "=", buyerId);
        List<BuyerAddress> list = dao.query(BuyerAddress.class, cnd);
        return list;
    }
}
